package com.yinhe.module.zxm.controller;

import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tebie.applib.api.APIContext;
import tebie.applib.api.IClient;

import javax.servlet.http.HttpServletResponse;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.ArrayList;

@RestController

@RequestMapping("/coreMail")
public class CoreMailLogin extends BaseController {

    final String userEmail  = "admin@example.com";  //登录的邮箱地址
    final String loginAttrs = "%3D";                //登录参数
    final String fid        = "1";                  //查看新邮件的数量


    @RequestMapping("mail")
    public AjaxResult userLogin(String userEmail, String loginAttrs, HttpServletResponse response) throws Exception{
        userEmail  = "admin@example.com";
        loginAttrs = "%3D";
        Socket socket = new Socket("10.100.37.9", 6195);
        IClient client = APIContext.getClient(socket);
        //System.out.println("woshi");
        try {
            APIContext result = client.userLoginEx(userEmail, loginAttrs);
            if (result.getRetCode() == APIContext.RC_NORMAL) {

                String encodedResult = result.getResult();
                String sid = getParameter(encodedResult, "sid");
                //System.out.println("sid是");
                String webname = getParameter(encodedResult, "webname");
                System.out.println(webname+"我是webName");
                // Coremail �� Webmail ��ҳ
                String mainURL = "http://10.100.37.9:9900" + "/coremail/XT6/index.jsp?sid=" + sid;
                // ���ص����¼����
                //redirect(mainURL);
                //System.out.println(mainURL+"我是url");
                ArrayList<String> list = new ArrayList<>();
                list.add(mainURL);
                return AjaxResult.success(list);
            } else {
                // 打印错误信息
                System.out.println("User login failed, code=" + result.getRetCode()
                        + ", msg=" + result.getErrorInfo());
            }
        } finally {
            client.close();
        }
        return AjaxResult.success(20);
    }
        //获取邮件信息
     @RequestMapping("newMail")
    public  static  int getNewMailCnt(String userEmail, String fid) throws Exception {
        Socket socket = new Socket("10.100.37.9", 6195);
        IClient client = APIContext.getClient(socket);
        try {
            String attr = "mbox_newmsgcnt"; // mbox_newmsgcnt Ϊ��ȡ�����ļ����µ�δ���ʼ�����
            if (fid != null && fid.length() > 0) {
                attr = "mbox.folder." + fid + ".newmsgcnt";
            }
            APIContext ret = client.getAttrs(userEmail, attr);
            if (ret.getRetCode() == APIContext.RC_NORMAL) {
                String result = getParameter(ret.getResult(), attr);
                if (result != null && result.length() > 0) {
                    System.out.println(result+"我是result");
                    return Integer.valueOf(result);
                }
            } else {
                System.out.println("Get new mail count failed, code=" + ret.getRetCode() + ", msg=" + ret.getErrorInfo());
            }
        } finally {
            client.close();
        }
        return -1;
    }

    //使用的获取参数
    private static String getParameter(String encodedResult, String key) throws Exception {
        int start;
        if (encodedResult.startsWith(key + '=')) {
            start = key.length() + 1;
        } else {
            int i = encodedResult.indexOf('&' + key + '=');
            if (i == -1) {
                return null;
            }
            start = i + key.length() + 2;
        }

        int end = encodedResult.indexOf('&', start);
        String value = (end == -1)
                ? encodedResult.substring(start)
                : encodedResult.substring(start, end);
        //System.out.println("我是vaule"+value);
        return URLDecoder.decode(value, "GBK");
    }
}
